package net.xju3_4.dao;

import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.entity.CarFavorite;

import java.util.List;

/**
 * 车辆收藏数据访问层
 */
public class CarFavoriteDao {
    
    /**
     * 添加收藏
     */
    public boolean addFavorite(Integer userId, Integer carId) {
        String sql = "INSERT INTO car_favorite (user_id, car_id) VALUES (?, ?)";
        try {
            int result = JdbcUtilsV1.executeUpdate(sql, userId, carId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 取消收藏
     */
    public boolean removeFavorite(Integer userId, Integer carId) {
        String sql = "DELETE FROM car_favorite WHERE user_id = ? AND car_id = ?";
        try {
            int result = JdbcUtilsV1.executeUpdate(sql, userId, carId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 检查是否已收藏
     */
    public boolean isFavorited(Integer userId, Integer carId) {
        String sql = "SELECT COUNT(*) FROM car_favorite WHERE user_id = ? AND car_id = ?";
        try {
            Object count = JdbcUtilsV1.executeQueryOneValue(sql, userId, carId);
            if (count != null) {
                return Integer.parseInt(count.toString()) > 0;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 获取用户收藏列表（分页）
     */
    public PageInfo<CarFavorite> getUserFavorites(Integer userId, int pageNum, int pageSize) {
        // 查询数据的SQL
        String sql = "SELECT " +
                "f.id, f.user_id AS userId, f.car_id AS carId, f.created_time AS createdTime, " +
                "c.brand AS carBrand, c.model AS carModel, c.license_plate AS licensePlate, " +
                "c.images AS carImages, c.daily_rent AS dailyRent, c.status AS carStatus " +
                "FROM car_favorite f " +
                "LEFT JOIN car c ON f.car_id = c.id " +
                "WHERE f.user_id = ? AND c.deleted = 0 " +
                "ORDER BY f.created_time DESC " +
                "LIMIT ?, ?";

        try {
            // 计算分页参数
            int offset = (pageNum - 1) * pageSize;

            // 执行查询获取数据列表
            List<CarFavorite> favorites = JdbcUtilsV1.executeQuery(CarFavorite.class, sql, userId, offset, pageSize);

            // 查询总数
            String countSql = "SELECT COUNT(*) FROM car_favorite f " +
                    "LEFT JOIN car c ON f.car_id = c.id " +
                    "WHERE f.user_id = ? AND c.deleted = 0";

            Object countObj = JdbcUtilsV1.executeQueryOneValue(countSql, userId);
            int count = countObj != null ? Integer.parseInt(countObj.toString()) : 0;

            // 创建分页信息
            return new PageInfo<>(pageNum, pageSize, count, favorites);

        } catch (Exception e) {
            e.printStackTrace();
            // 返回空的分页信息
            return new PageInfo<>(pageNum, pageSize, 0, new java.util.ArrayList<>());
        }
    }
    
    /**
     * 获取车辆的收藏数量
     */
    public int getFavoriteCount(Integer carId) {
        String sql = "SELECT COUNT(*) FROM car_favorite WHERE car_id = ?";
        try {
            Object count = JdbcUtilsV1.executeQueryOneValue(sql, carId);
            if (count != null) {
                return Integer.parseInt(count.toString());
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    /**
     * 获取用户收藏总数
     */
    public int getUserFavoriteCount(Integer userId) {
        String sql = "SELECT COUNT(*) FROM car_favorite f " +
                "LEFT JOIN car c ON f.car_id = c.id " +
                "WHERE f.user_id = ? AND c.deleted = 0";
        try {
            Object count = JdbcUtilsV1.executeQueryOneValue(sql, userId);
            if (count != null) {
                return Integer.parseInt(count.toString());
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    /**
     * 根据收藏ID删除收藏（管理功能）
     */
    public boolean deleteFavoriteById(Integer favoriteId) {
        String sql = "DELETE FROM car_favorite WHERE id = ?";
        try {
            int result = JdbcUtilsV1.executeUpdate(sql, favoriteId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
